<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 后台管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/patient/add-new' }">患者信息管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: './' }">基本信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加患者</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 添加数据的表单 -->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">

      <el-form-item label="住院号" prop="patientId">
        <el-input v-model="ruleForm.patientId"></el-input>
      </el-form-item>
      <el-form-item label="科室" prop="dept">
      <el-input v-model="ruleForm.dept"></el-input>
    </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="床号" prop="bedId">
        <el-input v-model="ruleForm.bedId"></el-input>
      </el-form-item>
      <el-form-item label="医生" prop="doctor">
        <el-input v-model="ruleForm.doctor"></el-input>
      </el-form-item>
      <el-form-item label="病症" prop="disease">
        <el-input type="textarea" v-model="ruleForm.disease"
                  :autosize="{ minRows: 3, maxRows: 5}"
                  maxlength="255" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="入院时间" prop="admissionTime">
        <el-input v-model="ruleForm.admissionTime"></el-input>
      </el-form-item>
      <el-form-item label="出院时间" prop="dischargeTime">
        <el-input v-model="ruleForm.dischargeTime"></el-input>
      </el-form-item>
      <el-form-item label="病症详情">
        <div id="wangEditor"></div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="gotoList">返回列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 父级类别的下拉菜单的选项数据
      //categoryOptions: [],
      // 表单
      ruleForm: {
        patientId: '',
        disease: '',
        dept:'',
        bedId:'',
        name:'',
        age:'',
        doctor:'',
        admissionTime:'',
        dischargeTime:''
      },
      // 表单规则
      rules: {
        patientId: [
          {required: true, message: '请填写住院号', trigger: 'change'}
        ],
        dept: [
          {required: true, message: '请填写科室', trigger: 'change'}
        ],
        bedId: [
          {required: true, message: '请填写床号', trigger: 'change'}
        ],
        name: [
          {required: true, message: '请填写姓名', trigger: 'change'}
        ],
        age: [
          {required: true, message: '请填写年龄', trigger: 'change'}
        ],
        admissionTime: [
          {required: true, message: '请填写入院时间', trigger: 'change'}
        ],
        doctor: [
          {required: true, message: '请填写负责医生', trigger: 'change'}
        ],

        disease: [
          {required: true, message: '请输入病症', trigger: 'blur'},
          {min: 2, max: 150, message: '长度在 2 到 150 个字符', trigger: 'blur'}
        ]

      },
      // 富文本编辑器
      editor: {}
    };
  },
  methods: {
    // 返回列表
    gotoList() {
      this.$router.push('/admin/patient');
    },
    // 初始化富文本编辑器
    initWangEditor() {
      this.editor = new this.wangEditor('#wangEditor');
      this.editor.config.uploadImgServer = 'http://localhost:9090/resources/upload/image/patient';
      this.editor.config.uploadFileName = 'file';
      this.editor.config.uploadImgHooks = {
        customInsert: function(insertImgFn, result) {
          insertImgFn(result.data.url);
        }
      }
      this.editor.config.zIndex = 1;
      this.editor.create();
    },
    // 上传成功后的回调

    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9980/patient/add-new';
          console.log('url = ' + url);

         // this.ruleForm.admissionTime=new Date(this.ruleForm.admissionTime);
         // this.ruleForm.dischargeTime=new Date(this.ruleForm.dischargeTime);

          this.ruleForm.diseaseDetail = this.editor.txt.html();
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios
             // .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              let message = '添加患者成功！';
              this.$alert(message, '提示', {
                confirmButtonText: '确定',
                callback: action => {
                  this.resetForm(formName);
                }
              });
            } else {
              let title = '操作失败';
              this.$alert(jsonResult.message, title, {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          }).catch(error => {
            let title = '错误';
            let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
            this.$alert(message, title, {
              confirmButtonText: '确定',
              callback: action => {
              }
            });
            console.log(error);
          });
        } else {
          console.log('请求参数格式错误！');
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.editor.txt.clear();
    },
    // 加载类别树的数据

  },
  mounted() {
    this.initWangEditor();
  }
}
</script>

<style>
.picture-uploader {
  float: left;
  width: 240px;
  height: 135px;
  margin-right: 20px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.picture-uploader .picture-uploader-icon {
  width: 240px;
  height: 80px;
  font-size: 58px;
  color: #8c939d;
  line-height: 80px;
  text-align: center;
}

.picture-uploader .picture-uploader-text {
  width: 240px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  font-size: 10px;
  color: #8c939d;
}

.picture-uploader .picture-uploader-text i {
  font-size: 10px;
  font-weight: bold;
  font-style: normal;
  color: #F56C6C;
}

.picture-preview {
  width: 240px;
  height: 135px;
  margin-right: 20px;
  border: 1px solid #e8e6e6;
  border-radius: 6px;
  float: left;
  display: inline;
  position: relative;
}

.picture-preview .image {
  width: 240px;
  height: 135px;
  border-radius: 6px;
}
</style>